package swimv2gm.client.abilities;

import java.io.*;
import java.util.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.AbilityManager;
import swimv2gm.bl.domain.*;

/**
 * Servlet incaricata di preparare l'elenco delle competenze presenti nel
 * sistema, sia per visualizzarne l'elenco completo che per ottenere quelle non
 * ancora dichiarate da un utente.
 * 
 * @author Gallesio Matteo
 */
public class AbilityList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		AbilityManager man;
		try {
			man = (AbilityManager)InitialContext.doLookup("AbilityManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		List<Ability> abilities;
		Long uid = (Long)(request.getSession().getAttribute("user"));
		if (uid == null)
			uid = -1L;

		String action = request.getParameter("action");

		if (action != null && action.equalsIgnoreCase("add")) {
			abilities = man.getNotDeclaredBy(uid);
		}
		else {
			abilities = man.getAll();
		}

		request.setAttribute("list",abilities);

		String nextPage;

		if (action != null && action.equalsIgnoreCase("add")) {
			nextPage = "/user/addAbility.jsp";
		}
		else {
			nextPage = "/search/allAbilities.jsp";
		}

		request.getRequestDispatcher(nextPage).forward(request,response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
